/**
*┌──────────────────────────────────────────────────────────────┐
*│　描    述：Article                                                    
*│　作    者：NetCoreCMS                                            
*│　版    本：1.0    模板代码自动生成                                                
*│　创建时间：2019-12-25 10:52:46                             
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│　命名空间： NetCoreCMS.Services                                  
*│　类    名： ArticleService                                    
*└──────────────────────────────────────────────────────────────┘
*/
using NetCoreCMS.Common;
using NetCoreCMS.IRepository;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NetCoreCMS.Services
{
    public class ArticleService: IArticleService
    {
        private readonly IArticleRepository _repository;
        private readonly IArticle_albumsRepository _albums;
        private readonly IArticle_goodsRepository _goods;
        private readonly IArticle_goods_specRepository _goods_spec;
        private readonly IUser_group_priceRepository _user_group_price;
        public ArticleService(IArticleRepository repository,IArticle_albumsRepository albums,IArticle_goodsRepository goods, IArticle_goods_specRepository goods_spec, IUser_group_priceRepository user_group_price)
        {
            _repository = repository;
            _albums = albums;
            _goods = goods;
            _goods_spec = goods_spec;
            _user_group_price = user_group_price;
        }
        /// <summary>
        /// 带其它信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Article GetModel(int id)
        {
            var model = _repository.Get(id);
            if (model == null)
                return null;
            var albums = _albums.GetList("where article_id=@id", new { id = id }).ToList();
            if (albums.Count > 0)
                model.albums = albums;
            var goods = _goods.GetList(id);
            if (goods.Count > 0)
            {
                model.goods = goods;
                for (int i = 0; i < model.goods.Count; i++)
                {
                    model.goods[i].group_prices = _user_group_price.GetList("where goods_id=" + model.goods[i].id).ToList();
                }
            }
            else
            {
                model.goods = new List<Article_goods>();
            }
            return model;
        }

        public bool UpdateField(int id, string strValue)
        {
            return _repository.UpdateField(id, strValue);
        }
        public List<Models.Article> GetList(int pageNumber, int rowsPerPage, string conditions, string orderby, object parameters = null)
        {
            return _repository.GetListPaged(pageNumber, rowsPerPage, conditions, orderby, parameters).ToList();
        }
        public int RecordCount(string conditions, object parameters = null)
        {
            return _repository.RecordCount(conditions, parameters);
        }
        /// <summary>
        /// 获得关健字查询分页数据(搜索用到)
        /// </summary>
        public List<Models.Article> ArticleSearch(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            strWhere = strWhere + " and datediff(d,add_time,getdate())>=0";
            recordCount = _repository.RecordCount(strWhere);
            return  _repository.GetListPaged(pageIndex, pageSize, strWhere, filedOrder).ToList();

        }
        public bool Delete(int id, Models.Sysconfig sysConfig, string env)
        {
            string content = GetContent(id);//获取信息内容
            bool result = _repository.DeleteAll(id,env);//删除内容
            if (result && !string.IsNullOrEmpty(content))
            {
                FileHelper.DeleteContentPic(content, "/" + sysConfig.filepath,env); //删除内容图片
            }
            return result;
        }
        /// <summary>
        /// 返回信息内容
        /// </summary>
        public string GetContent(int article_id)
        {
            string content = GetModel(article_id).content;
            if (string.IsNullOrEmpty(content))
            {
                return string.Empty;
            }
            return content;
        }

        public bool Update(Article model,string env)
        {
            try
            {
                #region 修改主表数据==========================
                _repository.Update(model);
                #endregion
                if (model.channel_id == 2)
                {
                    #region 修改图片相册==========================
                    //删除/添加/修改相册图片
                    _albums.Update(model.albums, model.id, env);
                    #endregion

                    #region 修改商品价格==========================
                    //删除旧商品价格
                    _goods.DeleteAll(model.id);
                    //添加商品价格
                    if (model.goods != null)
                    {
                        foreach (Models.Article_goods modelt in model.goods)
                        {
                            _goods.Add(modelt, model.id);
                        }
                    }
                    #endregion

                    #region 修改商品规格==========================
                    //先删除旧的规格
                    _goods_spec.DeleteArticleId(model.id);
                    //添加商品规格
                    if (model.specs != null)
                    {
                        foreach (Models.Article_goods_spec modelt in model.specs)
                        {
                            Article_goods_spec spec = new Article_goods_spec();
                            spec = modelt;
                            spec.article_id = model.id;
                            _goods_spec.Insert(spec);
                        }
                    }
                    #endregion
                }
                return true;
            }
            catch
            {                
                return false;
            }
        }

        public int Add(Article model)
        {
            int id=(int)_repository.Insert(model);
            if (model.channel_id == 2)
            {
                //albums
                if(model.albums!=null)
                for (int i = 0; i < model.albums.Count; i++)
                {
                    model.albums[i].article_id = id;
                    _albums.Insert(model.albums[i]);
                }
                //goods
                if (model.goods != null)
                    for (int i = 0; i < model.goods.Count; i++)
                {
                    model.goods[i].article_id = id;
                    _goods.Add(model.goods[i], id);
                }
                //specs
                if (model.specs != null)
                    for (int i = 0; i < model.specs.Count; i++)
                {
                    model.specs[i].article_id = id;
                    _goods_spec.Insert(model.specs[i]);
                }
            }
            return id;
        }

        public bool Exists(int id)
        {
            return GetModel(id) != null ? true : false;
        }

        public int GetStockQuantity(int article_id, int goods_id)
        {
            if (goods_id > 0)
            {
                return _goods.GetList("where article_id=@article_id and id=@goods_id",new { article_id=article_id,goods_id=goods_id}).FirstOrDefault().stock_quantity;
            }
            else
            {
                return _repository.Get(article_id).stock_quantity;
            }
        }

        public async Task<Article> GetClick(int article_id)
        {
            return await _repository.GetAsync(article_id);
        }

        public List<Article> GetList(int top, string strWhere, string orderBy)
        {
            return _repository.GetList(strWhere + " order by " + orderBy).Take(top).ToList();
        }
    }
}